package basis;

import java.util.Scanner;

/**
 * 奇数阶魔方
 * 
 * @author 71936_000
 * 
 */
public class Demo4_MagicSquare {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入奇数阶");
		int n = sc.nextInt();
		while (n < 3 || n % 2 == 0) {
			System.out.println("程序错误,请输入奇数");
			n = sc.nextInt();
		}
		int[][] a = new int[n][n];
		int num = n * n;
		int i = 0;
		int j = n / 2;
		a[i][j] = 1; // 确定1的摆放位置
		for (int x = 2; x <= num; x++) {
			int p = i;
			int q = j;
			i = i - 1;
			j = j + 1;
			if (i < 0) { // 判断是否到第一行
				i = n - 1;
			}
			if (j > n - 1) { // 判断是否到最后一列
				j = 0;
			}
			if (a[i][j] != 0) { // 判断下一个数的位置有没有被占用
				i = p + 1;
				j = q;
			}
			a[i][j] = x;
		}
		for (i = 0; i < n; i++) { // 遍历二维数组，打印魔方
			for (j = 0; j < n; j++) {
				System.out.print(a[i][j] + "\t");
			}

			System.out.println();
		}
	}

}
